package com.feiuxiu.datastructure.linklist;


import java.util.NoSuchElementException;

/**
 * description: 双向链表实现
 * author: feixiu
 * date:2025/9/9 11:03
 */
public class MyLinkedList<E> {
    //虚拟头尾节点
    final private Node<E> head,tail;

    private int size;
    private static class Node<E>{
        E val;
        Node<E> next;
        Node<E> prev;

        public Node(E val) {
            this.val = val;
        }
    }

    // 构造函数初始化虚拟头尾节点
    public MyLinkedList() {
        this.head=new Node<>(null);
        this.tail=new Node<>(null);
        head.next=tail;
        tail.prev=head;
        this.size=0;
    }

    // 增
    public void  addLast(E e){
        Node<E> x=new Node<>(e);
        Node<E> lastEle=tail.prev;
        lastEle.next=x;
        x.prev=lastEle;
        x.next=tail;
        tail.prev=x;
        size++;
    }

    public void  addFirst(E e){
        Node<E> x=new Node<>(e);
        Node<E> firstData=head.next;
        head.next=x;
        x.prev=head;
        x.next=firstData;
        firstData.prev=x;
        size++;
    }

    public void add(int index,E element){
        // 判断插入位置是否合法
        checkPositionIndex(index);
        if (index==size){
            addLast(element);
            return;
        }
        Node<E> p= getNode(index);
        Node<E> temp = p.prev;

        Node<E> x = new Node<>(element);

        p.prev=x;
        x.next=p;

        temp.next=x;
        x.prev=temp;

        size++;
    }




    //删除


    public E removeFirst(){
        //判断是不是空
        if (size==0){
            throw new NoSuchElementException();
        }
        Node<E> first = head.next;
        Node<E> temp = first.next;

        head.next=temp;
        temp.prev=head;

        first.prev=null;
        first.next=null;

        size--;
        return first.val;
    }

    public E removeLast(){
        //判断是不是空
        if (size==0){
            throw new NoSuchElementException();
        }
        Node<E> last = tail.prev;
        Node<E> temp = last.prev;

        temp.next=tail;
        tail.prev=temp;

        last.prev=null;
        last.next=null;

        size--;
        return last.val;

    }

    public E remove(int index){
        checkElementIndex(index);
        Node<E> target = getNode(index);
        Node<E> prev = target.prev;
        Node<E> next = target.next;
        prev.next=target.next;
        next.prev=prev;
        target.next=null;
        target.prev=null;
        size--;
        return target.val;

    }




    //查
    public E get(int index){
        checkElementIndex(index);
        Node<E> x = getNode(index);
        return x.val;
    }
    public E getFirst(){
        checkElementIndex(0);
        return head.next.val;

    }
    public E getLast(){
        checkElementIndex(0);
        return tail.prev.val;
    }


    //修改
    public E set(int index,E element){
        checkElementIndex(index);
        Node<E> x = getNode(index);
        E oldVal=x.val;
        x.val=element;
        return oldVal;
    }


        //工具

    private Node<E> getNode(int index) {
        checkElementIndex(index);
        Node<E> p = head.next;

        for (int i = 0; i < index; i++) {
            p=p.next;
        }
        return p;
    }

    private boolean isElementIndex(int index) {
        return index >= 0 && index < size;
    }

    private boolean isPositionIndex(int index) {
        return index >= 0 && index <= size;
    }

    //检查index 索引位置是否可以存在元素
    private void checkElementIndex(int index){
        if (!isElementIndex(index))
            throw new IndexOutOfBoundsException("Index: "+index +", Size: "+size);
    }
    //检查index 索引位置是否可以添加元素
    private void checkPositionIndex(int index){
        if (!isPositionIndex(index))
            throw new IndexOutOfBoundsException("Index: "+index +", Size: "+size);
    }
    private void display() {
        System.out.println("size = " + size);
        for (Node<E> p = head.next; p != tail; p = p.next) {
            System.out.print(p.val + " <-> ");
        }
        System.out.println("null");
        System.out.println();
    }

    public static void main(String[] args) {
        MyLinkedList<Integer> list = new MyLinkedList<>();
        list.addLast(1);
        list.addLast(2);
        list.addLast(3);
        list.addFirst(0);
        list.add(4, 100);

        list.display();
        // size = 5
        // 0 <-> 1 <-> 100 <-> 2 -> 3 -> null
    }

}
